home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / test / execf_test1.c < prev    next >
C/C++ Source or Header  |  1996-07-07  |  2KB  |  112 lines

  1. /* Example program to test execf() */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <errno.h>
  9.  
  10. typedef unsigned char bool;
  11.  
  12. #define FALSE 0
  13. #define TRUE  1
  14.  
  15. static char *program = "child_fun";
  16.  
  17. static char msgbuf[512];
  18.  
  19. void
  20. LogFatal(char *x0, char *x1)
  21. {
  22.     extern char *sys_errlist[];
  23.     static bool entered = FALSE;
  24.  
  25.     if (entered)
  26.         return;
  27.     entered = TRUE;
  28.  
  29.     fprintf(stderr, "%s: ", program);
  30.     if (errno)
  31.         fprintf(stderr, "%s: ", sys_errlist[ errno ]);
  32.     fprintf(stderr, x0,x1);
  33.     fprintf(stderr, "  Stop.\n");
  34.     exit(20);
  35. }
  36.  
  37. void
  38. LogFatalI(char *s, int i)
  39. {
  40.     /*NOSTRICT*/
  41.     LogFatal(s, (char *)i);
  42. }
  43.  
  44.  
  45. int process1(void *data)
  46. {
  47.    FILE *out;
  48.    char buff[256];
  49.    int ret;
  50.    
  51.    out = fopen("CON:10/120/520/80/New Process 1", "r+");
  52.    if (out == NULL)
  53.       return -1;
  54.  
  55.    fprintf(out, "I got \"%s\"\n", data);
  56.    fprintf(out, "Hit Return to Close Window\n");
  57.    fflush(out);
  58.  
  59.    if (!fgets(buff, sizeof(buff), out)) {
  60.       strcpy(msgbuf, "fgets() failed!");
  61.       fclose(out);
  62.       return -1;
  63.    }
  64.  
  65.    fclose(out);
  66.  
  67.    if (strcmp(buff, "\n") == 0) {
  68.       strcpy(msgbuf, "I'm returning 0 to you");
  69.       ret = 0;
  70.    } else {
  71.       buff[strlen(buff)-1] = 0;
  72.       sprintf(msgbuf, "You doesn't simply hit return, but wrote: \"%s\"\n", buff);
  73.       strcat(msgbuf, "so I'm returning the number of characters you wrote");
  74.       ret = strlen(buff);
  75.    }
  76.  
  77.    return ret;
  78. }
  79.  
  80.  
  81. main(int argc, char **argv)
  82. {
  83.    int pid;
  84.    int status;
  85.    int rc;
  86.    char *msg;
  87.    
  88.    if (argc >= 2)
  89.        msg = argv[1];
  90.    else
  91.        msg = "Hello out there!";
  92.    /* Start process 1 */
  93.    printf("Starting process 1\n");
  94.    pid = execf(process1, msg, -1, -1, NULL, -1);
  95.    if (pid < 0)
  96.        LogFatal("Cannot exec %s.", program);
  97.    /* Simply wait child completion */
  98.    printf("Waiting for process1 to finish\n");
  99.    rc =  wait(&status);
  100.    printf("Return from child is %d\n", WEXITSTATUS(status));
  101.    printf("Message from child is:\n%s\n", msgbuf);
  102.    if (rc > 0) {
  103.       if (WIFSIGNALED(status))
  104.           LogFatalI("Signal %d.", WTERMSIG(status));
  105.       if (WIFEXITED(status) && WEXITSTATUS(status))
  106.           LogFatalI("Exit code %d.", WEXITSTATUS(status));
  107.    }
  108.    
  109.  
  110.    return(0);
  111. }
  112.